package org.smartercontextmobile.communication;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class CallSCLogin implements Runnable {

	private final String actionSoap = "http://www.smartercontext.org/contextmgr/communication/ExternalLLogin";
	private final String method = "login";
	private final String namespace = "http://www.smartercontext.org/contextmgr/communication/";
	private final String url = "http://192.168.160.59:8080/SmarterContext_Web_Server/services/Communication_Impl?wsdl";
	public boolean statusConnection = false;
	private String JSONdata;
	private boolean authentication;
	public String ret="";
	
	public CallSCLogin(String JSONdata){
		this.JSONdata = JSONdata;
		this.authentication = false;
	}
	
	
	
	@Override
	public void run() {
		
		try {
		
		//Request
		SoapObject request = new SoapObject(namespace, method);
		PropertyInfo property = new PropertyInfo();
		property.setName("credentials");
		property.setValue(JSONdata);
		property.setType(String.class);
		request.addProperty(property);
		
		PropertyInfo property2 = new PropertyInfo();
		property2.setName("format");
		property2.setValue("json");
		property2.setType(String.class);
		request.addProperty(property2);
		
		//Envelope model
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		
		//Transport
		HttpTransportSE transport = new HttpTransportSE(url);
		
		//Call
		
			transport.call(actionSoap, envelope);
			SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
			if(result.toString()!=null){
				ret=result.toString();
				if(ret.equals("true")){
					authentication = true;
				}else if(ret.equals("false")){
					authentication = false;
				}
			}
			statusConnection = true;
			
			
		} catch (IOException e) {
			statusConnection = true;
		} catch (XmlPullParserException e) {
			statusConnection = true;
		}
				
		
	}



	public boolean isAuthentication() {
		return authentication;
	}
	
	
	
	

}
